﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace LeSoft.Application.Web.Areas.FinancialManage.Controllers
{
    [HandleError]
    public class ReceiptBillController : MvcControllerBase
    {
        //
        // GET: /FinancialManage/ReceiptBill/Index
        private ReceiptBillBLL bll = new ReceiptBillBLL();
        private PStructCache pstructCache = new PStructCache();
        private OrganizeCache organizeCache = new OrganizeCache();
        #region 视图

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Form()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [HandlerAction(OperationType.GetForm, "收款单", "GetReceiptBillInfo")]
        public ActionResult GetReceiptBillInfo(string keyValue)
        {
            var entity = bll.GetEntity(keyValue);
            var CustomerInfo = new CustomerInfoBLL().GetEntity(entity.CustomerId);
            var PStructInfo = new PStructBLL().GetEntity(entity.PStructId);
            var list = bll.GetDetailList(keyValue);
            var data = new
            {
                BillInfo = entity,
                CustomerInfo = CustomerInfo,
                PStructInfo = PStructInfo,
                list = list
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAction(OperationType.GetList, "收款单", "GetPageList")]
        public ActionResult GetPageList(Pagination pagination, string queryJson)
        {
            var queryParam = queryJson.ToJObject();
            if (!queryParam["PStructId"].IsEmpty())
            {
                queryParam["PStructId"] = pstructCache.GetSonStructId(queryParam["PStructId"].ToString());
            }
            if (!queryParam["OrganzieId"].IsEmpty())
            {
                queryParam["OrganzieId"] = organizeCache.GetSonOrganizeId(queryParam["OrganzieId"].ToString());
            }
            var Json = queryParam.ToString();
            var data = bll.GetPageList(pagination, Json);

            foreach (var item in data)
            {
                item.CustomerId = new CustomerInfoBLL().GetEntity(item.CustomerId).IsEmpty() ? "" : new CustomerInfoBLL().GetEntity(item.CustomerId).Name;
                item.PStructId = new PStructBLL().GetEntity(item.PStructId).IsEmpty() ? "" : new PStructBLL().GetEntity(item.PStructId).AllName;
                item.PayWay = bll.GetDataItemDetailName(item.PayWay);
                item.InvoiceType = bll.GetDataItemDetailName(item.InvoiceType);
            }
            if (!queryParam["CustomerName"].IsEmpty())
            {
                string CustomerName = queryParam["CustomerName"].ToString();
                data = data.Where(e => e.CustomerId.Contains(CustomerName));
            }
            var watch = CommonHelper.TimerStart();
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());

        }
        #endregion

        #region 保存数据

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "收款单", "SaveForm")]
        public ActionResult SaveForm(string keyValue, string queryJson,string Lst)
        {
            List<ReceiptBillDetailEntity> lst = new List<ReceiptBillDetailEntity>();
            lst = Lst.ToObject<List<ReceiptBillDetailEntity>>(); //应收明细
            try
            {
                bll.SaveForm(keyValue, queryJson,lst);
                return Success("保存成功");
            }
            catch (Exception e)
            {
                return Error(e.Message);
                throw;
            }

        }

        /// 删除实体 
        public ActionResult RemoveForm(string keyValue)
        {
            try
            {
                bll.RemoveForm(keyValue);
                return Success("操作成功");
            }
            catch (Exception e)
            {
                return Error(e.Message);
                throw;
            }

        }

        #endregion
    }
}
